\documentclass[12pt,oneside]{amsart}
\usepackage{geometry} % see geometry.pdf on how to lay out the page. There's lots.
\geometry{a4paper} % or letter or a5paper or ... etc
\usepackage{graphicx}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{float}
\lstset{stepnumber=1, frame=single,language=matlab}

\title{Advanced Image Processing - Lab 1 - Image analysis in frequency domain - Report}
\author{Waldemar Franczak}

\begin{document}
\maketitle
\tableofcontents

\section{Introduction}
The aim of this laboratory was to practice image processing with use of Fourier Transform and investigate the visible effects of applying different FT filtering techniques. This report discusses conducted experiments and presents examples of results obtained in the course of work.
\section{Experiment 1 - Phase and magnitude manipulation}
First task of this laboratory was the investigation of phase and magnitude components of image's Fourier Transform. 
\subsection{Phase manipulation}
Figure 1 presents original image and the phase component obtained by using \emph{phase\_only()} function. Function calculates the Fourier Transform of the image, computes it's phase and performs inverse Fourier Transform creating phase image as an output.
\begin{figure}[H]
\begin{tabular}{c c}
  \includegraphics[scale=0.45]{images/circuit}
  \includegraphics[scale=0.4]{images/circuitPhase}
\end{tabular}
\label{circuit}
\caption{Original image of a circuit(left) and it's phase}
\end{figure}
As we know from the lecture phase component contains the information about where particular frequency lies in the image. In practice it means that it contains edge information of the image. We can notice on the phase image, that eventhough the color information is lost, we are still able to distinguish the structure of circuit components. On the contrary figure 2 presents the result of changing the phase component of the image using \emph{random\_phase()} function.
\begin{figure}[H]
\begin{tabular}{c c}
  \includegraphics[scale=0.4]{images/lena}
  \includegraphics[scale=0.4]{images/lenaPhase}
\end{tabular}
\label{lena}
\caption{Original image of Lena(left) and it's processed version with random phase component}
\end{figure}
We can observe that color information is somewhat maintained however the contour information is completely lost. Visualy image with changed phase component does not resemble original image at all. We are able to to determine that visualy, because original image holds clear content information which is a woman. Figure 3 gives an example of situation where it might not be so obvious.
\begin{figure}[H]
\begin{tabular}{c c}
  \includegraphics[scale=0.4]{images/sonar}
  \includegraphics[scale=0.4]{images/sonarPhase}
\end{tabular}
\label{sonar}
\caption{Original image of sonar signal(left) and it's processed version with random phase component}
\end{figure}
As we can see it's hard to determine the change with respect to original image since due to particular characteristic of the image, they look very similar. However we can notice slight change in image intensity due to the use of random distribution for phase manipulation. This way we obtain balance in contours distribution which results in brighter image comparing to the original image.
\subsection{Magnitude manipulation}
Figure 4 presents a result of manipulating the magnitude component of Fourier Transform. 

\begin{figure}[H]
\begin{tabular}{c c}
  \includegraphics[scale=0.4]{images/lena}
  \includegraphics[scale=0.4]{images/lenaMag}
\end{tabular}
\label{lenaMag}
\caption{Original image of Lena(left) and it's processed version with random magnitude component}
\end{figure}

As we know magnitude component hold information about particular freqnecy intensity on the image. Figure shows us that it allows us to manipulate with intensities of the image parts. Due to phase component left intact we can distinguish contours on the processed image. If we take into consideration sonar image again(Figure 5), we can see that again there might be a problem in determining the difference visually. In terms of image intensity, we observe opposite change in intensity, in comparison to phase manipulation - the image is darker, due to random values distribution.

\begin{figure}[H]
\begin{tabular}{c c}
  \includegraphics[scale=0.4]{images/sonar}
  \includegraphics[scale=0.4]{images/sonarMag}
\end{tabular}
\label{sonarMag}
\caption{Original image of sonar signal(left) and it's processed version with random magnitude component}
\end{figure}

\section{Experiment 2.1 - Simple Fourier Filtering}
In this experiment we investigated filtering in Fourier domain using \emph{SimpleFiltering()} function. In order to perform filtering for particular function we use process of convolution to obtain result. Fourier Transform allows us to use simple element-wise multiplication. \emph{SimpleFiltering()} function creates a low/high pass filter by creating a centered mask where inside/outside circle elements are set to zero. Figure 6 presents the high-pass filter and result of applying it to image of Lena.

\begin{figure}[H]
\begin{tabular}{c c}
  \includegraphics[scale=0.4]{images/lenaHfilt}
  \includegraphics[scale=0.4]{images/lenaHres}
\end{tabular}
\label{highpass}
\caption{High-pass filter(left) and result of filtering. Cutoff frequency 0.3.}
\end{figure}

High frequencies of the spectrum correspond to edges of the image and as we can see the contours were preserved after filtering while the 'filling' information for image regions is lost. Figure 7 presents low-pass filter and result of applying it to the same image.

\begin{figure}[H]
\begin{tabular}{c c}
  \includegraphics[scale=0.4]{images/lenaLfilt}
  \includegraphics[scale=0.4]{images/lenaLres}
\end{tabular}
\label{lowpass}
\caption{Low-pass filter(left) and result of filtering. Cutoff frequency 0.3.}
\end{figure}

Low frequencies corresponding to the filling are preserved so we can distinguish different gray-level regions however image seems to be blured due to lose of contour information contained in high frequencies of the spectrum.
\newline\newline
Problem connected with this type of filtering is aliasing. Changes performed in frequency domain may cause that the signal in time domain will not fit single period. In this case signal spills over from one period into the adjacent periods. 

\section{Experiment 2.2 - Developing Fast-Fourier Transform}
This task included implementing a function that allows to perform image filtering by the means of 2D Fourier transform, for the given input image. Figure 8 presents the code of the function.
\begin{figure}
  \begin{lstlisting}
function out = twoDimDFT(inImg,range)
%Function calculates 2D Discrete
%Fourier Transform and performs filtering
%using gaussian filter matrix.
out = [];

dftImg= fftshift(fft2(inImg));

filtFunc = fspecial('gaussian',
          [size(inImg,1) size(inImg,2)],range);

filtImg = dftImg.*filtFunc;

out = real(ifft2(ifftshift(filtImg)));

subplot(1,3,1)
imshow(mat2gray(out))
subplot(1,3,2)
imshow(inImg)
subplot(1,3,3)
imshow(abs(filtImg))

end
  \end{lstlisting}
  \caption{\emph{twoDimDFT()} function}
\end{figure}

As a filter function we use gaussian distribution which sigma is dependent on \emph{range} parameter passed to the function. Function displays processed image, original image and it's spectrum(figure 9).

\begin{figure}[H]
\begin{tabular}{c}
  \includegraphics[scale=0.9]{images/twoDim}
\end{tabular}
\label{twodim}
\caption{From the left: processed image, original image and spectrum of processed image}
\end{figure}
We observe subtle smoothing effect on the image which visualy seems more attractive that the one in case of low-pass filter.
\section{Experiment 2.3 Fourier Spectrum and Average Value}
image a
\begin{figure}[H]
\begin{tabular}{c}
  \includegraphics[scale=0.5]{images/imageA}
\end{tabular}
\label{imagea}
\caption{\emph{imageA} spectrum}
\end{figure}
DC term of the spectrum which is the zero-frequency element corresponds to average brightness of the image. So from the spectrum we can easily compute average brightness by dividing DC term by number of elements in the image. In case of \emph{imageA} it gives:\newline
avgVal =  207.3147
\section{Experiment 3 - Compression and DCT}
The last experiment was to investigate how the quantization affects the image. For that purpose we use \emph{quant\_fft()} function which for input image limits quantization levels to desired number. In order to determine which FT component is more sensitive to quantization we were first increasing quantization levels equally in order to get result similar to original image(figure 11).

\begin{figure}[H]
\begin{tabular}{c}
  \includegraphics[scale=0.7]{images/circ}\\
  \includegraphics[scale=0.7]{images/circAp}
\end{tabular}
\label{circ}
\caption{First row: Original image, FT's magnitude and phase components. Second row presents result with limited quantization to 10000 levels.}
\end{figure}

Next step was to decrease quantization levels for phase component to small level, and next do the same with magnitude component to compare the results. Results comparison is presented on figure 12.
\begin{figure}[H]
\begin{tabular}{c}
  \includegraphics[scale=0.7]{images/circP}\\
  \includegraphics[scale=0.7]{images/circM}
\end{tabular}
\label{circ2}
\caption{First row: quantization levels for phase set to 10(magnitude: 10000). Second row: quantization levels for magnitude set to 10(phase: 10000)}
\end{figure}

We can conclude that magnitude component is strongly affected by quantization while the phase component stays intact event for large changes in quantization. The reason is hidden in the range of values that each of the components can take, where for magnitude scale is quite large so it more sensitive to changes.

\section{Conclusions}
During the course of this laboratory we performed multiple exercises regarding using Fourier Transform as a tool for image processing. We get a better understanding of significance of each component of the frequency spectrum as well as how it can be manipulated for the purpose of affecting visual characteristic of an image in spatial domain.
\end{document}
